﻿/*****************************************************************
 * Copyright (C) Knights Warrior Corporation. All rights reserved.
 * 
 * Author:   圣殿骑士（Knights Warrior） 
 * Email:    KnightsWarrior@msn.com
 * Website:  http://www.cnblogs.com/KnightsWarrior/       http://knightswarrior.blog.51cto.com/
 * Create Date:  5/8/2010 
 * Usage:
 *
 * RevisionHistory
 * Date         Author               Description
 * 
*****************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;

namespace KnightsWarriorAutoupdater
{
    class CommonUnitity
    {
        public static string SystemBinUrl = AppDomain.CurrentDomain.BaseDirectory;

        public static void RestartApplication()
        {
            Process.Start(Application.ExecutablePath);
            Environment.Exit(0);
        }

        #region 原有模式
        //public static string GetFolderUrl(DownloadFileInfo file)
        //{
        //    string folderPathUrl = string.Empty;
        //    int folderPathPoint = file.DownloadUrl.IndexOf("/", 15) + 1;
        //    string filepathstring = file.DownloadUrl.Substring(folderPathPoint);
        //    int folderPathPoint1 = filepathstring.IndexOf("/");
        //    string filepathstring1 = filepathstring.Substring(folderPathPoint1 + 1);
        //    if (filepathstring1.IndexOf("/") != -1)
        //    {
        //        string[] ExeGroup = filepathstring1.Split('/');
        //        for (int i = 0; i < ExeGroup.Length - 1; i++)
        //        {
        //            folderPathUrl += "\\" + ExeGroup[i];
        //        }
        //        if (!Directory.Exists(SystemBinUrl + ConstFile.TEMPFOLDERNAME + folderPathUrl))
        //        {
        //            Directory.CreateDirectory(SystemBinUrl + ConstFile.TEMPFOLDERNAME + folderPathUrl);
        //        }
        //    }
        //    return folderPathUrl;
        //}
        #endregion

        public static string GetFolderUrl(DownloadFileInfo file)
        {
            string folderPathUrl = string.Empty;
            int folderPathPoint = file.DownloadUrl.IndexOf("/", 15) + 1;
            string filepathstring = file.DownloadUrl.Substring(folderPathPoint);
            //int folderPathPoint1 = filepathstring.IndexOf("/");
            //string filepathstring1 = filepathstring.Substring(folderPathPoint1 + 1);
            //if(filepathstring1.IndexOf("/") != -1)
            if (filepathstring.IndexOf("/") != -1)
            {
                //string[] ExeGroup = filepathstring1.Split('/');
                string[] ExeGroup = filepathstring.Split('/');
                for (int i = 0; i < ExeGroup.Length - 1; i++)
                {
                    folderPathUrl += "\\" + ExeGroup[i];
                }
                if (!Directory.Exists(SystemBinUrl + ConstFile.TEMPFOLDERNAME + folderPathUrl))
                {
                    Directory.CreateDirectory(SystemBinUrl + ConstFile.TEMPFOLDERNAME + folderPathUrl);
                }
            }
            return folderPathUrl;
        }

        /// <summary>
        /// 获取文件md5
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        private static string GetMD5HashFromFile(string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Open);
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString(@"x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception(@"GetMD5HashFromFile() fail, error:" +ex.Message);
            }
        }
    }
}
